package com.oceansoft.core.jxkh.dao.impl;

import com.hui.platform.system.exception.HuiException;
import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.util.DatetimeUtil;
import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.dao.IEvaluationDao;
import com.oceansoft.core.jxkh.dao.IWorkLogDao;
import com.oceansoft.core.jxkh.domain.*;
import com.oceansoft.core.jxkh.service.IWorkLogService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/24.
 */
@Repository
public class WorkLogDaoImpl implements IWorkLogDao {


    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public Pagination<WorkLog> querybycondition(HashMap<String, String> map, SysUser user) throws HuiException {
        int pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        String sql = "select * from t_jxkh_worklog t where 1=1 ";
        if (map.containsKey("policeNum") && StringUtil.isNotNullAndBlank(map.get("policeNum"))) {
            sql += " and policenum = '" + map.get("policeNum") + "'";
        }
        if (map.containsKey("year") && StringUtil.isNotNullAndBlank(map.get("year"))) {
            sql += " and ( substr(TO_CHAR(t.WORKSTARTTIME,'YYYY-MM-DD HH24:MI:SS'),0,4) ='" + map.get("year") + "' ";
            sql += " or substr(TO_CHAR(t.WORKENDTIME,'YYYY-MM-DD HH24:MI:SS'),0,4) ='" + map.get("year") + "')";
        }
        if (map.containsKey("month") && StringUtil.isNotNullAndBlank(map.get("month"))) {
            sql += " and ( substr(TO_CHAR(t.WORKSTARTTIME,'YYYY-MM-DD HH24:MI:SS'),6,2) ='" + map.get("month") + "' ";
            sql += " or substr(TO_CHAR(t.WORKENDTIME,'YYYY-MM-DD HH24:MI:SS'),6,2) ='" + map.get("month") + "')";
        }
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), pageIndex);
        List<WorkLog> list = jdbcTemplate.query(sql, new RowMapper<WorkLog>() {
            @Override
            public WorkLog mapRow(ResultSet rs, int arg1) throws SQLException {
                WorkLog log = new WorkLog();
                log.setDeptid(rs.getString("DEPTID"));//部门名称
                log.setDbid(rs.getInt("DBID"));
                log.setType(rs.getString("TYPE"));
                log.setPoliceNum(rs.getString("POLICENUM"));
                log.setUpdatetime(rs.getDate("UPDATETIME"));
                log.setDeptid(rs.getString("DEPTID"));
                log.setRecordtime(rs.getString("RECORDTIME"));
                log.setUsername(rs.getString("USERNAME"));
                log.setWorkstartdate(rs.getString("WORKSTARTDATE"));
                log.setWorkenddate(rs.getString("WORKENDDATE"));
                return log;
            }
        });
        System.out.println(sql);
        return new Pagination<WorkLog>(list, _total, pageIndex);
    }

    @Override
    public int saveDailywork(Daliywork daliywork) throws HuiException {
        int i = jdbcTemplate.queryForInt("select SEQ_T_JXKH_DAILYLOG.NEXTVAL from dual");
        String sql = "insert into T_JXKH_DAILYLOG (  DBID ," +
                "  POLICENUM    ," +
                "  WORKDATE     ," +
                "  CREATETIME ) values (" + i + ", '" + daliywork.getPoliceNum() + "',to_date('" + daliywork.getWorkdate() + "','YYYY-MM-DD'),sysdate)";
        System.out.println(sql);
        jdbcTemplate.update(sql);
        return i;
    }

    @Override
    public List<Daliywork> weekDaliywork(HashMap<String, String> map) throws HuiException {
        String sql = "select * from T_JXKH_DAILYLOG t where 1=1 ";
        if (map.containsKey("policeNum") && StringUtil.isNotNullAndBlank(map.get("policeNum"))) {
            sql += " and policenum = '" + map.get("policeNum") + "'";
        }
        if (map.containsKey("startDate") && StringUtil.isNotNullAndBlank(map.get("startDate"))) {
            sql += " and t.WORKDATE>=TO_DATE('" + map.get("startDate") + "','YYYY-MM-DD HH24:MI:SS')  AND t.WORKDATE>=TO_DATE('" + map.get("startDate") + "','YYYY-MM-DD HH24:MI:SS')";
        }
        if (map.containsKey("endDate") && StringUtil.isNotNullAndBlank(map.get("endDate"))) {
            sql += " and t.WORKDATE <= TO_DATE('" + map.get("endDate") + "','YYYY-MM-DD HH24:MI:SS')";
        }
        sql += " order by t.WORKDATE asc";
        List<Daliywork> list = jdbcTemplate.query(sql, new RowMapper<Daliywork>() {
            @Override
            public Daliywork mapRow(ResultSet rs, int arg1) throws SQLException {
                Daliywork e = new Daliywork();
                e.setPoliceNum(rs.getString("POLICENUM"));
                // e.setDbid(rs.getInt("DBID"));
                return e;
            }
        });
        return list;
    }

    @Override
    public int saveWeekwork(WorkLog workLog) throws HuiException {
        int i = jdbcTemplate.queryForInt("select seq_t_jxkh_worklog.nextval from dual");
        jdbcTemplate.update("insert into T_JXKH_WorkLog (  DBID                 ," +
                        "  DEPTID               ," +
                        "  USERNAME             ," +
                        "  POLICENUM            ," +
                        "  CREATETIME           ," +
                        "  TYPE                 ," +
                        "  UPDATETIME           ," +
                        "  WORKSTARTDATE        ," +
                        "  WORKENDDATE ) VALUES (?,?,?,?,sysdate,?,sysdate,to_date(?,'YYYY-MM-DD'),to_date(?,'YYYY-MM-DD'))", i, workLog.getDeptid(), workLog.getUsername(), workLog.getPoliceNum(),
                workLog.getType(), workLog.getWorkstartdate(), workLog.getWorkenddate());
        return i;
    }

    @Override
    public List<WorkLog> monthsWeekwork(HashMap<String, String> map) throws HuiException {
        String sql = "select * from T_JXKH_WorkLog t where 1=1 and type='1' ";
        if (map.containsKey("policeNum") && StringUtil.isNotNullAndBlank(map.get("policeNum"))) {
            sql += " and policenum = '" + map.get("policeNum") + "'";
        }
        if (map.containsKey("month") && StringUtil.isNotNullAndBlank(map.get("month"))) {
            sql += " and ( substr(TO_CHAR(t.WORKSTARTTIME,'YYYY-MM-DD HH24:MI:SS'),6,2) ='" + map.get("month") + "' ";
            sql += " or substr(TO_CHAR(t.WORKENDTIME,'YYYY-MM-DD HH24:MI:SS'),6,2) ='" + map.get("month") + "')";
        }
        sql += " order by t.WORKENDTIME asc";
        List<WorkLog> list = jdbcTemplate.query(sql, new RowMapper<WorkLog>() {
            @Override
            public WorkLog mapRow(ResultSet rs, int arg1) throws SQLException {
                WorkLog e = new WorkLog();
                e.setPoliceNum(rs.getString("POLICENUM"));
                e.setDbid(rs.getInt("DBID"));
                return e;
            }
        });
        return list;
    }

    @Override
    public void saveDailyworkcontent(Daliyworkcontent content) throws HuiException {
        String sql = "insert into T_JXKH_DAILYLOG_CONTENT (DBID ,CONTENT , DAILYID ,TYPE) values(SEQ_T_JXKH_DAILYLOG_CONTENT.NEXTVAL,?,?,?)";
        jdbcTemplate.update(sql, content.getContent(), content.getDailyid(), content.getType());
    }

    @Override
    public Pagination<WorkLog> myWorkList(HashMap<String, String> map, SysUser user) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select t.dbid,t.policenum,trunc(t.WORKSTARTDATE) WORKSTARTDATE,trunc(t.WORKENDDATE) WORKENDDATE,(select workitem from t_jxkh_worklog_content c where c.logid = t.dbid and c.type ='2' and rownum=1) workitem from t_jxkh_worklog t where t.policenum = '" + user.getPoliceNum() + "' ";
        if (map.containsKey("start") && com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(map.get("start"))) {
            sql += "and ( WORKSTARTDATE <=to_date('" + map.get("start") + "','YYYY-MM-DD') ";
            sql += "OR WORKENDDATE >to_date('" + map.get("start") + "','YYYY-MM-DD') )";
        }
        if (map.containsKey("end") && com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(map.get("end"))) {
            sql += "and ( WORKSTARTDATE <=to_date('" + map.get("end") + "','YYYY-MM-DD') ";
            sql += "OR WORKENDDATE >=to_date('" + map.get("end") + "','YYYY-MM-DD') )";
        }
        if (map.containsKey("type") && com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(map.get("type"))) {
            sql += "and type='"+map.get("type")+"'";
        }
        sql += "order by t.workstartdate desc";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List<WorkLog> list = jdbcTemplate.query(page_sql, new RowMapper<WorkLog>() {
            @Override
            public WorkLog mapRow(ResultSet rs, int arg1) throws SQLException {
                WorkLog log = new WorkLog();
                log.setDbid(rs.getInt("dbid"));
                log.setPoliceNum(rs.getString("POLICENUM"));
                // log.setType(rs.getString("TYPE"));
                log.setWorkstartdate(rs.getString("WORKSTARTDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(log.getWorkstartdate())){
                    log.setWorkstartdate(log.getWorkstartdate().replace("00:00:00.0",""));
                }

                log.setWorkenddate(rs.getString("WORKENDDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(log.getWorkenddate())){
                    log.setWorkenddate(log.getWorkenddate().replace("00:00:00.0",""));
                }

                log.setWorkitem(rs.getString("WORKITEM"));
                return log;
            }
        });
        System.out.println(sql);
        return new Pagination<WorkLog>(list, _total, (offset/limit)+1);
    }

    @Override
    public void saveWorkLogContent(WorkLogcontent content)throws HuiException  {
        jdbcTemplate.update("insert into T_JXKH_WORKLOG_CONTENT (  DBID     ,  LOGID    ,  TYPE  ,WORKITEM )values(seq_t_jxkh_worklog_content.NEXTVAL,?,?,?)", content.getLogid(), content.getType(), content.getWorkitem());
    }

    @Override
    public List<Map<String, Object>> findLastWeekWork(String policeNum,String type) {
        String sql = " select t1.workdate," +
                "       (select wm_concat(t2.content)" +
                "          from T_JXKH_DAILYLOG_CONTENT t2" +
                "         where t2.dailyid = t1.dbid\n" +
                "           and t2.type = '1') content1,(select wm_concat(t2.content)" +
                "          from T_JXKH_DAILYLOG_CONTENT t2" +
                "         where t2.dailyid = t1.dbid" +
                "           and t2.type = '2') content2," +
                "           (select wm_concat(t2.content)" +
                "          from T_JXKH_DAILYLOG_CONTENT t2" +
                "         where t2.dailyid = t1.dbid" +
                "           and t2.type = '3') content3" +
                "  from T_JXKH_DAILYLOG t1 where T1.WORKDATE>=(trunc(sysdate,'iw') - 7) AND T1.WORKDATE<=(trunc(sysdate,'iw') - 1) ";
        sql+=" AND T1.POLICENUM='"+policeNum+"' order by t1.workdate asc";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<Map<String, Object>> findLastMonthWork(String policeNum, String type) {
        String sql ="select trunc(t1.WORKSTARTDATE) WORKSTARTDATE,trunc(T1.WORKENDDATE) WORKENDDATE," +
                "       (select wm_concat(t2.workitem)" +
                "          from t_jxkh_worklog_content t2" +
                "         where t2.logid = t1.dbid" +
                "           and t2.type = '1') content1,(select wm_concat(t2.workitem)" +
                "          from t_jxkh_worklog_content t2" +
                "         where t2.logid = t1.dbid" +
                "           and t2.type = '2') content2," +
                "           (select wm_concat(t2.workitem)" +
                "          from t_jxkh_worklog_content t2" +
                "         where t2.logid = t1.dbid" +
                "           and t2.type = '3') content3" +
                "  from t_jxkh_worklog t1 where 1=1";
        if ("2".equals(type)) {
            sql+=" and ((T1.WORKSTARTDATE between trunc(add_months(sysdate, -1), 'mm') and trunc(last_day(add_months(sysdate, -1))) ) or (T1.WORKENDDATE between trunc(add_months(sysdate, -1), 'mm') and trunc(last_day(add_months(sysdate, -1))) ) )";
        }else  if ("3".equals(type)) {
            sql+=" and ((T1.WORKSTARTDATE between (TRUNC(SYSDATE, 'Q') - 1) and add_months(TRUNC(SYSDATE, 'Q'), -3) ) or (T1.WORKENDDATE between (TRUNC(SYSDATE, 'Q') - 1) and add_months(TRUNC(SYSDATE, 'Q'), -3) ) )";
        }
        sql+=" AND T1.POLICENUM='"+policeNum+"' order by t1.WORKSTARTDATE asc";
        System.out.println(sql);
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<Daliyworkcontent> dailyWorkContent(String policeNum, String workdate, String type) throws HuiException  {
        String sql = "select c.content,c.type,c.dbid,c.DAILYID from t_Jxkh_Dailylog_Content c , t_jxkh_dailylog d where c.dailyid=d.dbid ";
        //and d.workdate = to_date('2017-11-08','YYYY-MM-DD') AND d.policenum ='zwj'
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(workdate)){
            sql+=" and d.workdate = to_date('"+workdate+"','YYYY-MM-DD')";
        }
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(policeNum)){
            sql+=" AND d.policenum ='"+policeNum+"'";
        }
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(type)){
            sql+=" AND c.type ='"+type+"'";
        }
        sql += " order by c.dbid asc";
        List<Daliyworkcontent> list = jdbcTemplate.query(sql, new RowMapper<Daliyworkcontent>() {
            @Override
            public Daliyworkcontent mapRow(ResultSet rs, int arg1) throws SQLException {
                Daliyworkcontent e = new Daliyworkcontent();
                e.setDbid(rs.getInt("DBID"));
                e.setDailyid(rs.getInt("DAILYID"));
                e.setContent(rs.getString("CONTENT"));
                e.setType(rs.getString("TYPE"));
                return e;
            }
        });
        return list;
    }

    @Override
    public void deleteDailyWorkContent(int dbid)throws HuiException   {
        String sql ="delete from t_Jxkh_Dailylog_Content where dailyid = "+dbid;
        jdbcTemplate.update(sql);
    }

    @Override
    public List<Map<String, Object>> findMonthWork(String policeNum) {
        String sql ="select trunc(t1.WORKSTARTDATE) WORKSTARTDATE,trunc(T1.WORKENDDATE) WORKENDDATE," +
                "       (select wm_concat(t2.workitem)" +
                "          from t_jxkh_worklog_content t2" +
                "         where t2.logid = t1.dbid" +
                "           and t2.type = '1') content1,(select wm_concat(t2.workitem)" +
                "          from t_jxkh_worklog_content t2" +
                "         where t2.logid = t1.dbid" +
                "           and t2.type = '2') content2," +
                "           (select wm_concat(t2.workitem)" +
                "          from t_jxkh_worklog_content t2" +
                "         where t2.logid = t1.dbid" +
                "           and t2.type = '3') content3" +
                "  from t_jxkh_worklog t1 where 1=1";
            sql+=" and t1.type='2'";
            sql+=" and ((T1.WORKSTARTDATE between trunc(add_months(sysdate, -1), 'mm') and trunc(last_day(add_months(sysdate, -1))) ) or (T1.WORKENDDATE between trunc(add_months(sysdate, -1), 'mm') and trunc(last_day(add_months(sysdate, -1))) ) )";
        sql+=" AND T1.POLICENUM='"+policeNum+"' order by t1.WORKSTARTDATE asc";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public int checkIsAdd(SysUser user, Map<String, String> map) {
        String sql = "select count(1) from  t_jxkh_worklog where policenum = '"+user.getPoliceNum()+"' and workstartdate = to_date('"+map.get("firstDay")+"','YYYY-MM-DD') and workenddate = to_date('"+map.get("lastDay")+"','YYYY-MM-DD')";
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public Daliywork findDaliyworkByDate(String policeNum, String workdate) {
        String sql="select * from t_jxkh_dailylog where 1=1";
        sql+=" and workdate = to_date('"+workdate+"','YYYY-MM-DD')";
        sql+=" and policenum = '"+policeNum+"'";
        List<Daliywork> list = jdbcTemplate.query(sql, new RowMapper<Daliywork>() {
            @Override
            public Daliywork mapRow(ResultSet rs, int arg1) throws SQLException {
                Daliywork e = new Daliywork();
                e.setPoliceNum(rs.getString("POLICENUM"));
                e.setDbid(rs.getInt("DBID"));
                return e;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }else{
            return  new Daliywork();
        }
    }

    @Override
    public WorkLog findWorkLog(int dbid) {
        String sql="select * from t_jxkh_dailylog where 1=1";
        sql+=" and dbid = "+dbid ;
        List<WorkLog> list = jdbcTemplate.query(sql, new RowMapper<WorkLog>() {
            @Override
            public WorkLog mapRow(ResultSet rs, int arg1) throws SQLException {
                WorkLog e = new WorkLog();
                e.setPoliceNum(rs.getString("POLICENUM"));
                e.setDbid(rs.getInt("DBID"));
                return e;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }else{
            return  new WorkLog();
        }
    }

    @Override
    public List<WorkLogcontent> weekWorkInfo(String policeNum, int dbid) {
        String sql ="select * from t_jxkh_worklog_content where LOGID = "+dbid;
        List<WorkLogcontent> list = jdbcTemplate.query(sql, new RowMapper<WorkLogcontent>() {
            @Override
            public WorkLogcontent mapRow(ResultSet rs, int arg1) throws SQLException {
                WorkLogcontent e = new WorkLogcontent();
                e.setLogid(rs.getInt("LOGID"));
                e.setType(rs.getString("TYPE"));
                e.setWorkitem(rs.getString("WORKITEM"));
                e.setDbid(rs.getInt("DBID"));
                return e;
            }
        });
        return list;
    }

    @Override
    public void deleteWeekworkContent(Integer dbid) {
        String sql ="delete from T_JXKH_WORKLOG_CONTENT where logid= "+dbid;
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteData(int dbid) {
        String sql ="delete from T_JXKH_WORKLOG where dbid= "+dbid;
        jdbcTemplate.update(sql);
    }

    @Override
    public Ydxj findYdxj(int dbid) {
        String sql ="select * from t_jxkh_Ydxj where dbid = "+dbid;
        List<Ydxj> list = jdbcTemplate.query(sql, new RowMapper<Ydxj>() {
            @Override
            public Ydxj mapRow(ResultSet rs, int arg1) throws SQLException {
                Ydxj e = new Ydxj();
               e.setWorkstartdate(rs.getString("WORKSTARTDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(e.getWorkstartdate())){
                    e.setWorkstartdate(e.getWorkstartdate().replace("00:00:00.0",""));
                }
                e.setWorkenddate(rs.getString("WORKENDDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(e.getWorkenddate())){
                    e.setWorkenddate(e.getWorkenddate().replace("00:00:00.0",""));
                }
                e.setContent(rs.getString("CONTENT"));
                e.setType(rs.getString("TYPE"));
                e.setDbid(rs.getInt("DBID"));
                return e;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }else{
             return  new Ydxj();
        }

    }

    @Override
    public Pagination<Ydxj> wdybList(HashMap<String, String> map, SysUser user) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        String sql = "select * from t_jxkh_ydxj t where t.policenum = '" + user.getPoliceNum() + "' ";
        if (map.containsKey("start") && com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(map.get("start"))) {
            sql += "and ( WORKSTARTDATE <=to_date('" + map.get("start") + "','YYYY-MM-DD') ";
            sql += "OR WORKENDDATE >to_date('" + map.get("start") + "','YYYY-MM-DD') )";
        }
        if (map.containsKey("end") && com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(map.get("end"))) {
            sql += "and ( WORKSTARTDATE <=to_date('" + map.get("end") + "','YYYY-MM-DD') ";
            sql += "OR WORKENDDATE >=to_date('" + map.get("end") + "','YYYY-MM-DD') )";
        }
        sql += "order by t.workstartdate desc";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List<Ydxj> list = jdbcTemplate.query(page_sql, new RowMapper<Ydxj>() {
            @Override
            public Ydxj mapRow(ResultSet rs, int arg1) throws SQLException {
                Ydxj log = new Ydxj();
                log.setDbid(rs.getInt("dbid"));
                log.setWorkstartdate(rs.getString("WORKSTARTDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(log.getWorkstartdate())){
                    log.setWorkstartdate(log.getWorkstartdate().replace("00:00:00.0",""));
                }
                log.setWorkenddate(rs.getString("WORKENDDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(log.getWorkenddate())){
                    log.setWorkenddate(log.getWorkenddate().replace("00:00:00.0",""));
                }
                log.setContent(rs.getString("content"));
                log.setPolicenum(rs.getString("POLICENUM"));
                return log;
            }
        });
        System.out.println(sql);
        return new Pagination<Ydxj>(list, _total, (offset/limit)+1);
    }

    @Override
    public void saveYdxj(Ydxj ydxj) throws HuiException {
        String sql=" insert into t_jxkh_ydxj (dbid,policenum,CONTENT,WORKSTARTDATE,WORKENDDATE,TYPE) values(SEQ_JXKH_YDXJ.NEXTVAL,?,?,to_date(?,'YYYY-MM-DD'),to_date(?,'YYYY-MM-DD'),?)";
        jdbcTemplate.update(sql,ydxj.getPolicenum(),ydxj.getContent(),ydxj.getWorkstartdate(),ydxj.getWorkenddate(),ydxj.getType());
    }

    @Override
    public Ydxj findLastYdxj(String policeNum) {
        Map<String,String> map  = DatetimeUtil.getLastMonth();
        String sql = "select * from t_jxkh_ydxj where policeNum = '"+policeNum+"'";
        sql+=" and workstartdate = to_date('"+map.get("firstDay")+"','YYYY-MM-DD')";
        List<Ydxj> list = jdbcTemplate.query(sql, new RowMapper<Ydxj>() {
            @Override
            public Ydxj mapRow(ResultSet rs, int arg1) throws SQLException {
                Ydxj log = new Ydxj();
                log.setDbid(rs.getInt("dbid"));
                log.setWorkstartdate(rs.getString("WORKSTARTDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(log.getWorkstartdate())){
                    log.setWorkstartdate(log.getWorkstartdate().replace("00:00:00.0",""));
                }
                log.setWorkenddate(rs.getString("WORKENDDATE"));
                if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank(log.getWorkenddate())){
                    log.setWorkenddate(log.getWorkenddate().replace("00:00:00.0",""));
                }
                log.setContent(rs.getString("content"));
                log.setPolicenum(rs.getString("POLICENUM"));
                return log;
            }
        });
        if(list.size()>0){
            return list.get(0);
        }
        return new Ydxj();
    }

    @Override
    public void deleteYdxj(int dbid) {
        String sql = "delete from t_jxkh_ydxj where dbid = "+dbid;
        jdbcTemplate.update(sql);
    }

    @Override
    public void editYdxj(Ydxj ydxj) {
        String sql = "update t_jxkh_ydxj set CONTENT = ? WHERE DBID = ?";
        jdbcTemplate.update(sql,ydxj.getContent(),ydxj.getDbid());
    }

    @Override
    public boolean checkIsAddyb(Map<String, String> map, SysUser user) {
        String sql ="select count(1) from t_jxkh_ydxj where workstartdate = to_date('"+map.get("firstDay")+"','YYYY-MM-DD')";
        int i = jdbcTemplate.queryForInt(sql);
        if(i>0){
            return true;
        }else{
            return false;
        }

    }
}
